#pragma once
#include <atlcrack.h>
#include "calctool.h"

#define SBB_LEFT	0xF000
#define SBB_MID		0xF001
#define SBB_RIGHT	0xF002


class CSelectionBar :
	public CWindowImpl<CSelectionBar, CToolBarCtrl>,
	public CCustomDraw<CSelectionBar>
{
protected:
	CFont	m_fontDef;			// Default font
	CMenu	m_menu[3];			// Menus for the buttons
	CString	m_strTitle[3],		// Button titles
			m_strOption[3];		// Button options

	// ResizeMid:
	//
	//		Resizes the hidden middle button
	//
	void ResizeMid()
	{
		CRect rcClient, rcLeft, rcRight;

		GetClientRect(&rcClient);
		GetItemRect(0, &rcLeft);
		GetItemRect(2, &rcRight);

		TBBUTTONINFO nfo;
		nfo.cbSize = sizeof(nfo);
		nfo.dwMask = TBIF_SIZE;

		nfo.cx = rcClient.Width() - rcLeft.Width() - rcRight.Width();

		SetButtonInfo(SBB_MID, &nfo);
	}

public:
	CSelectionBar()
	{
	}

	// SetText
	//
	//		Sets the button's text
	//
	void SetText(UINT nID, LPCTSTR pszText)
	{
		TBBUTTONINFO	nfo;

		nfo.cbSize	= sizeof(nfo);
		nfo.dwMask	= TBIF_TEXT | TBIF_STATE;
		nfo.pszText = const_cast<LPTSTR>(pszText);
		nfo.fsState	= TBSTATE_ENABLED;

		SetButtonInfo(nID, &nfo);
		ResizeMid();
	}

	// SetOption
	//
	//		Use to set the selected menu option (text when the menu is not displayed)
	//
	void SetOption(UINT nID, LPCTSTR pszOption, BOOL bSetBtn = TRUE)
	{
		int	i = nID - SBB_LEFT;

		ATLASSERT(i >= 0 && i < 3);

		m_strOption[i] = pszOption;

		if(bSetBtn)
			SetText(nID, pszOption);
	}

	// SetTitle
	//
	//		Use to set the menu title (text when the menu is displayed)
	//
	void SetTitle(UINT nID, LPCTSTR pszTitle)
	{
		int	i = nID - SBB_LEFT;

		ATLASSERT(i >= 0 && i < 3);

		m_strTitle[i] = pszTitle;
	}

	BOOL SetMenu(UINT nID, LPCTSTR pszMenuRes)
	{
		int		i = nID - SBB_LEFT;

		ATLASSERT(i >= 0 && i < 3);

		m_menu[i].DestroyMenu();
		return m_menu[i].LoadMenu(pszMenuRes);
	}

	// ShowMenu
	//
	//		Shows the menu for the given button index (0, 1, 2)
	//
	void ShowMenu(int iMenu)
	{
		if(m_menu[iMenu].m_hMenu)
		{
			CMenuHandle	menu = m_menu[iMenu].GetSubMenu(0);
			CRect		rc;

			SetText(SBB_LEFT + iMenu, m_strTitle[iMenu]);

			GetItemRect(iMenu, &rc);
			ClientToScreen(&rc);

			if(iMenu)
				menu.TrackPopupMenu(TPM_RIGHTALIGN | TPM_TOPALIGN,
							rc.right, rc.bottom, GetParent());
			else
				menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_TOPALIGN,
							rc.left, rc.bottom, GetParent());

			SetText(SBB_LEFT + iMenu, m_strOption[iMenu]);
		}
	}

	// Create:
	//
	//		Creates the selection bar window
	//
	HWND Create(HWND hWndParent)
	{
		HWND	hWnd;

		hWnd = CommandBar_Create(_Module.GetModuleInstance(), hWndParent, ATL_IDW_TOOLBAR);
		if(hWnd != NULL)
		{
			TBBUTTON	btn[3];
			LOGFONT		lf;
			CClientDC	dc(hWnd);

			lf.lfHeight			= -muldiv(8, dc.GetDeviceCaps(LOGPIXELSY), 72); //-11;
			lf.lfWidth			= 0;
			lf.lfEscapement		= 0;
			lf.lfOrientation	= 0;
			lf.lfWeight			= FW_BOLD;
			lf.lfItalic			= FALSE;
			lf.lfUnderline		= FALSE;
			lf.lfStrikeOut		= 0;
			lf.lfCharSet		= ANSI_CHARSET;
			lf.lfOutPrecision	= OUT_DEFAULT_PRECIS;
			lf.lfClipPrecision	= CLIP_DEFAULT_PRECIS;
			lf.lfQuality		= DEFAULT_QUALITY;
			lf.lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
			lstrcpy(lf.lfFaceName, TEXT("Tahoma"));

			if(!m_fontDef.IsNull())
				m_fontDef.DeleteObject();
			m_fontDef.CreateFontIndirect(&lf);

			memset(&btn, 0, sizeof(btn));

			btn[0].iBitmap =
			btn[1].iBitmap = 
			btn[2].iBitmap = I_IMAGENONE;

			btn[0].idCommand	= SBB_LEFT;
			btn[1].idCommand	= SBB_MID;
			btn[2].idCommand	= SBB_RIGHT;

			btn[0].fsState	=
			btn[2].fsState	= TBSTATE_HIDDEN;
			btn[1].fsState	= 0;//TBSTATE_INDETERMINATE;

			btn[0].fsStyle	=
			btn[2].fsStyle	= TBSTYLE_DROPDOWN | TBSTYLE_AUTOSIZE;

			::SendMessage(hWnd, CCM_SETVERSION, COMCTL32_VERSION, 0);

			SubclassWindow(hWnd);

			SetFont(m_fontDef);
			AddButtons(3, btn);
			ResizeMid();
		}
		return hWnd;
	}

	BEGIN_MSG_MAP(CSelectionBar)
		MSG_WM_SIZE(OnSize)
		CHAIN_MSG_MAP_ALT(CCustomDraw<CSelectionBar>, 1)

		REFLECTED_COMMAND_RANGE_HANDLER	(SBB_LEFT, SBB_RIGHT, OnClick)
		REFLECTED_NOTIFY_HANDLER		(ATL_IDW_TOOLBAR, TBN_DROPDOWN, OnDropDown)

		DEFAULT_REFLECTION_HANDLER()
	END_MSG_MAP()


	DWORD OnPrePaint(int /*idCtrl*/, LPNMCUSTOMDRAW /*lpNMCustomDraw*/)
	{
		return CDRF_NOTIFYITEMDRAW;
	}


	DWORD OnItemPrePaint(int /*idCtrl*/, LPNMCUSTOMDRAW /*lpNMCustomDraw*/)
	{
		return CDRF_NOVERTBAR | CDRF_NOTIFYPOSTPAINT;
	}


	// OnItemPostPaint
	//
	//		Draw the bottom line
	//
	DWORD OnItemPostPaint(int /*idCtrl*/, LPNMCUSTOMDRAW lpNMCustomDraw)
	{
		CDCHandle	dc(lpNMCustomDraw->hdc);
		CRect		rc(lpNMCustomDraw->rc);

		dc.MoveTo(rc.left, rc.bottom);
		dc.LineTo(rc.right, rc.bottom);
		return CDRF_DODEFAULT;
	}

	// OnClick
	//
	//		A toolbar menu was clicked
	//
	LRESULT OnClick(WORD wNotifyCode, WORD nID, HWND hwndCtl, BOOL& bHandled)
	{
		if(nID == SBB_LEFT || nID == SBB_RIGHT)
		{
			ShowMenu(nID - SBB_LEFT);
		}

		bHandled = TRUE;
		return 0;
	}


	// OnDropDown
	//
	//		The user clicked the drop down arrow
	//
	LRESULT OnDropDown(int nID, LPNMHDR pNMHDR, BOOL& bHandled)
	{
		NMTOOLBAR*	pNMTB = (NMTOOLBAR*) pNMHDR;
		CRect		rc;

		if(pNMTB->iItem == SBB_LEFT || pNMTB->iItem == SBB_RIGHT)
		{
			ShowMenu(pNMTB->iItem - SBB_LEFT);
		}

		bHandled = TRUE;
		return 0;
	}

	// OnSize
	//
	//		The toolbar was resized
	//
	LRESULT OnSize(UINT state, CSize Size)
	{
		ResizeMid();

		return 0;
	}
};
